package com.scau.ums.controller;

import com.scau.ums.mapper.UserDIYMapper;
import com.scau.ums.model.Info;
import com.scau.ums.service.imp.info.DefaultSendInfoImp;
import com.scau.ums.service.imp.password.CommonPasswordImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author xfy
 * @version 1.0
 * @date 2019/8/6 0:06
 */
@Controller
public class ResetPasswordByAdminController {
    @Autowired
    UserDIYMapper userDIYMapper;
    @Autowired
    CommonPasswordImp commonPasswordImp;
    @Autowired
    DefaultSendInfoImp defaultSendInfoImp;

    @RequestMapping("/resetPassword")
    @ResponseBody
    public String resetPassword(HttpServletRequest request){
        String userType = request.getParameter("userType");
        String id = request.getParameter("id");
        System.out.println("用户类型：" + userType + " id：" + id);
        //重置密码时对用户的id进行加密存数据库
        String passwordEncoded = commonPasswordImp.encode(id);
        Info info;
        try {
            switch (userType){
                case "student":
                    userDIYMapper.resetStudentPasswordById(id, passwordEncoded);
                     info = defaultSendInfoImp.createInfo("密码重置","",id,"学生",
                            "您的密码已被成功重置为学号~");
                    defaultSendInfoImp.sendInfo(info);
                    break;
                case "teacher":
                    userDIYMapper.resetTeacherPasswordById(id, passwordEncoded);
                     info = defaultSendInfoImp.createInfo("密码重置","",id,"老师",
                            "您的密码已被成功重置为工号~");
                    defaultSendInfoImp.sendInfo(info);
                    break;
                case "maintainer":
                    userDIYMapper.resetMaintainerPasswordById(id, passwordEncoded);
                    info = defaultSendInfoImp.createInfo("密码重置","",id,"维修工",
                            "您的密码已被成功重置为工号~");
                    defaultSendInfoImp.sendInfo(info);
                    break;
                default:
                    throw new Exception();
            }
        }catch (Exception e){
            System.out.println(e.getStackTrace());
            return "resetPasswordFailed";
        }
        return "resetPasswordSuccessfully";
    }
}
